package com.example.demo.mapper;

import com.example.demo.model.Notice;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Optional;

@Mapper
public interface NoticeMapper {

    @Select("SELECT * FROM notice WHERE id = #{id}")
    Optional<Notice> findById(Long id);

    @Select("SELECT * FROM notice")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "creatorId", column = "creator_id"),
            @Result(property = "noticeTopic", column = "topic"),
            @Result(property = "content", column = "content"),
            @Result(property = "createTime", column = "create_time")
    })
    List<Notice> findAll();

    @Insert("INSERT INTO notice (creator_id, topic, content, create_time) VALUES (#{creatorId}, #{noticeTopic}, #{content}, #{createTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertNotice(Notice notice);

    @Delete("DELETE FROM notice WHERE id = #{id}")
    int deleteById(Long id);
}